package com.biz.hunter.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.biz.hunter.db.entity.TbForm;
import com.biz.hunter.dbutil.BaseDAO;


@Repository
public class TbFormDAO {
	private static final String SQL_GET_ID = "select * from tb_formid where wxapptype=1";
	
	private static final String SQL_SAVE="insert into tb_formid(formid,expire) VALUES(?,?)";
	
	private static final String SQL_DELETE="delete from tb_formid where formid=?";
	
	private static final String SQL_GET_UNIONID="select c.openid from tb_person a,userinfo b,tb_formid c where a.openid=b.openid and b.unionid=c.unionid and a.id=? limit 0,1";
	
	public List<TbForm> getAll() {
		return  BaseDAO.queryForList(TbForm.class, SQL_GET_ID);
	}
	
	public long save(TbForm form) {
		if (form == null) {
			return -1;
		}

		Long result = BaseDAO.save(SQL_SAVE, form.getFormid(),form.getExpire());
		if (result != null) {
			return result.longValue();
		}
		return -1;
	}
	
	public int delete(String formid) {
		if (formid==null) {
			return -1;
		}
		return BaseDAO.execute(SQL_DELETE, formid);
	}
	
	public TbForm getUnionidByPerson(int personid){
		return BaseDAO.queryForObject(TbForm.class, SQL_GET_UNIONID, personid);
	}
}
